<template>
  <a-card :bordered='false'>
    <div class='card-title'>{{ $route.meta.title }}</div>
    <div class='table-operator'>
      <a-button v-if='!customer_type' v-action:add type='primary' icon='plus' @click='handleAdd'>录入成交客户</a-button>

      <div style='margin: 10px 0'>
        关键词搜索：
        <a-input-search
          style='max-width: 360px; min-width: 150px;'
          v-model='queryParam.search'
          placeholder='请输入客户姓名/身份证号/公司名称/授权签约人/手机号码'
          @search='onSearch'
        />
        <span style='padding: 0 10px'></span>
        业务类型：
        <a-radio-group v-model='queryParam.customer_identity' button-style='solid' @change='handleRefresh(true)'>
          <a-radio-button :value='-1'>
            全部
          </a-radio-button>
          <a-radio-button :value='0'>
            个人
          </a-radio-button>
          <a-radio-button :value='1'>
            公司
          </a-radio-button>
        </a-radio-group>
        <span style='padding: 0 10px'></span>
        公司类型：
        <a-radio-group v-model='queryParam.corporate_type' button-style='solid' @change='handleRefresh(true)'>
          <a-radio-button :value='-1'>
            全部
          </a-radio-button>
          <a-radio-button :value='0'>
            个体户
          </a-radio-button>
          <a-radio-button :value='1'>
            小规模纳税人
          </a-radio-button>
          <a-radio-button :value='2'>
            一般纳税人
          </a-radio-button>
        </a-radio-group>
        录入时间：
        <a-range-picker format='YYYY-MM-DD' v-model='queryParam.create_time' @change='handleRefresh(true)' />
      </div>
    </div>
    <s-table
      ref='table'
      rowKey='customer_id'
      :loading='isLoading'
      :columns='columns'
      :data='loadData'
      :pageSize='15'
      :scroll='{x:1220}'
    >
      <div slot='source' slot-scope='text, item'>
        <a-tag v-if='item.source'>{{ item.source.source_name }}</a-tag>
        <span v-else>-</span>
      </div>
      <div slot='customer_identity' slot-scope='text, item'>
        <a-tag v-if='text==0' color='orange'>个人</a-tag>
        <a-tag v-if='text==1' color='blue'>公司</a-tag>
      </div>
      <div slot='corporate_type' slot-scope='text, item'>
        <a-tag v-if='text==0' color='green'>个体户</a-tag>
        <a-tag v-if='text==1' color='cyan'>小规模纳税人</a-tag>
        <a-tag v-if='text==2' color='blue'>一般纳税人</a-tag>
      </div>
      <div slot='khxx' slot-scope='text, item'>
        <div>客户姓名：{{ item.full_name || '-' }}</div>
        <div>身份证号：{{ item.idnumber || '-' }}</div>
        <div>公司名称：{{ item.corporate_name || '-' }}</div>
        <div>授权签约人：{{ item.signatory || '-' }}</div>
        <div>手机号码：{{ item.mobile || '-' }}</div>
      </div>
      <div class='actions' slot='action' slot-scope='text, item'>
        <a v-action:service_list @click='handleServiceList(item)'>服务列表</a>
        <a v-action:edit @click='handleEdit(item)'>编辑</a>
        <a v-action:delete @click='handleDelete(item)'>删除</a>
      </div>
    </s-table>
    <AddForm ref='AddForm' @handleSubmit='handleRefresh' />
    <PackageModel ref='PackageModel' @handleSubmit='handleRefresh' />
  </a-card>
</template>

<script>
import * as Api from '@/api/customer'
import { STable } from '@/components'
import AddForm from './modules/AddForm.vue'
import PackageModel from '../PackageModel/Index.vue'
import PropTypes from 'ant-design-vue/lib/_util/vue-types'
// import EditForm from './modules/EditForm'

export default {
  props: {
    customer_type: PropTypes.number.def(0)
  },
  name: 'Index',
  components: {
    STable,
    AddForm,
    PackageModel
    // EditForm
  },
  data() {
    return {
      // 查询参数
      queryParam: {
        customer_type: this.customer_type,
        customer_identity: -1,
        corporate_type: -1,
        create_time: ''
      },
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: 'ID',
          width: 60,
          dataIndex: 'customer_id'
        },
        {
          title: '业务类型',
          width: 100,
          dataIndex: 'customer_identity',
          scopedSlots: { customRender: 'customer_identity' }
        },
        {
          title: '客户信息',
          width: 340,
          dataIndex: 'khxx',
          scopedSlots: { customRender: 'khxx' }
        },
        {
          title: '客户来源',
          dataIndex: 'source',
          width: 120,
          scopedSlots: { customRender: 'source' }
        },
        {
          width: 120,
          title: '公司类型',
          dataIndex: 'corporate_type',
          scopedSlots: { customRender: 'corporate_type' }
        },
        {
          width: 200,
          title: '客户区域',
          dataIndex: 'region_text'
        },
        {
          width: 120,
          title: '录入用户',
          dataIndex: 'store_user.real_name'
        },
        {
          width: 180,
          title: '录入时间',
          dataIndex: 'create_time'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.list({ ...param, ...this.queryParam })
          .then(response => {
            return response.data.list
          })
      },
      types: [
        { text: '收款金额*', symbols: '%' },
        { text: '固定金额', symbols: '元' },
        { text: '每月提成', symbols: '元' }
      ],
      types2: [
        { text: '(成交价格-成本价格)*', symbols: '%' },
        { text: '固定金额', symbols: '元' },
        { text: '每月提成', symbols: '元' }
      ],
      types3: [
        { text: '收款金额*', symbols: '%' },
        { text: '固定金额', symbols: '元' },
        { text: '选择成本费用', symbols: '元' }
      ]

    }
  },
  created() {
  },
  methods: {
    // 新增记录
    handleAdd() {
      this.$refs.AddForm.add()
    },

    // 编辑记录
    handleEdit(item) {
      this.$refs.AddForm.edit(item)
    },

    // 查看服务列表
    handleServiceList(item) {
      this.$refs.PackageModel.queryParam.customer_id = item.customer_id
      this.$refs.PackageModel.open(item)
    },

    // 删除记录
    handleDelete(item) {
      const app = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk() {
          return Api.deleted({ apiId: item['customer_id'] })
            .then(result => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally(result => modal.destroy())
        }
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh(bool = false) {
      this.$refs.table.refresh(bool)
    },

    // 检索查询
    onSearch() {
      this.handleRefresh(true)
    }

  }
}
</script>
